var $LIST=[],$HOMECAT="0", $TOTALNEWS=0;
window.onload = function(){
	$POSMENU=1;
	listNewsCat(-1);
	new CHECKLOGIN().check();
	clickSubTab(0);
	
}
function listHome(homeid){
	new NEWSORDER().listHome(homeid);
}
function deleteNews(newsid){
//	if($TOTALNEWS<=14){
//		alert("Không thể xóa. Tối thiểu 14 tin");
//		return;
//	}
	new NEWSORDER().deleteNews(newsid);
}
function updatePosition(){
	var pos = "";
	for(var i=0; i < $LIST.length; i++){
		pos+=$LIST[i]+"-"+$N("news_"+$LIST[i])[0].value+",";
	}
	if(pos!=""){
		pos = pos.substring(0,pos.length-1);
	}
	new NEWSORDER().updatePosition(pos);
}
function addHome(type){
	var param = $N("newsid")[0].value;
	var newsid = param.split("/")[5];

	new NEWSORDER().addHome(newsid,type);
}
NEWSORDER = function(){}
NEWSORDER.prototype.listHome=function(homeid){
	$("loading").style.visibility = "visible";
	var fncSuc = XHR.Delegate(this,this.listHomeSuc);
	var param = {action:"listHome",catId:homeid};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.listHomeSuc=function(r){
	$("loading").style.visibility = "hidden";
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		var il = xml.getElementsByTagName("news");
		$LIST=[];
		var noImg="";
		var homepic = "";
		var bgcolor = "",thumb="homepic",id="",pichome="";
		$TOTALNEWS=0;
		for(var i=0; i < il.length; i++){
			$TOTALNEWS++;
			id = il[i].getAttribute("newsid");
			thumb="thumbnail";
			if(il[i].getAttribute("homepic")=="null"){
				homepic="";
				noImg="<span style='color:red'>Chưa có ảnh lớn</span>";
			}else{
				homepic = il[i].getAttribute("homepic");
				noImg="";
			}
			pichome = "<div id=\"homepic_"+id+"\" style=\"padding-left:50px\">"+listHomePic(id,homepic)+"</div>" ;
			html += "<div class=\'list1\' "+bgcolor+"  id=\"div_"+id+"\">" 
					+ "<table width=\'600\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'>"
					+ "	  <tr>"
					+ "	    <td width=\'110\'>" 
					+ "        <div style=\'width:100px;height:80px;overflow:hidden\'><img src=\'"+genLinkImg(id,il[i].getAttribute(thumb))+"\' style=\'width:100px\'></div>"
					+ "     </td>"
					+ "	    <td>"
					+ "			<img src=\'images/deleteimg.gif\' style=\'cursor:pointer;vertical-align:bottom\' onClick=\'deleteNews(\""+id+"\")\'> "	
					+ "			<b><a target=\'_blank\' href=\'"+makeLinkViewNews(il[i].getAttribute("newsid"),il[i].getAttribute("utitle"))+"\' style=\'color:black\'>"+il[i].getAttribute("title")+"&nbsp;&nbsp;"+noImg+"</a></b>"
					+ "			<div style=\'font-size:11px;padding-left:10px\'>Thêm bởi: "+il[i].getAttribute("poster")+"&nbsp;&nbsp;&nbsp;&nbsp;Ngày "+fulldateSQLtoN(il[i].getAttribute("createddate"))+"</div>"		
					+ "			<div style=\'font-size:11px;padding-left:10px\'>"
					+ "           <a target=\'_blank\' href=\'"+il[i].getAttribute("link")+"\'>[Xem trang gốc]</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a  target=\'_blank\' href=\'"+makeLinkViewNews(id,il[i].getAttribute("utitle"))+"\'>[Xem ROLO News]</a></div>"							
					+ "			Vị trí: <input type=\'text\' value=\'"+(i+1)+"\' name=\'news_"+il[i].getAttribute("newsid")+"\' id=\'news_"+il[i].getAttribute("newsid")+"\' style=\'width:40px\' /> "		
					+ "			&nbsp;&nbsp;&nbsp;<span id=\'updateBtn_"+id+"\' style=\'cursor:pointer;padding: 3px; background-color: rgb(153, 204, 153); color: rgb(255, 255, 255); font-size: 11px;\' onClick=\'showUpload(\""+id+"\",\""+homepic+"\")\'>[Upload Hình lớn] </span> "							
					+ "		</td>"
					+ "	  </tr>"
					+ "   <tr>" 
					+ "		<td colspan=\"2\">"
					+ pichome
					+ "   </td>"		
					+ "   </tr>"
					+ "</table>"
					+"</div>";
			$LIST[$LIST.length]=il[i].getAttribute("newsid");
		}
		if(html!=""){
			$("listnews").innerHTML=html;
		}else{
			$("listnews").innerHTML="<div align='center' style='padding:10px'><b>Chưa có dữ liệu!</b></div>";
		}
		$("totalnews").innerHTML = il.length;
	}
}
NEWSORDER.prototype.addHome=function(newsId,type){
	$("loading").style.visibility = "visible";
	var fncSuc = XHR.Delegate(this,this.addHomeSuc);
	var param = {action:"addHome",newsId:newsId,catId:$CURENTCAT,type:type};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.addHomeSuc=function(r){
	$("loading").style.visibility = "hidden";
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		
		listHome($CURENTCAT);
	}
}
NEWSORDER.prototype.publish=function(catid){
	$("loading").style.visibility = "visible";
	var fncSuc = XHR.Delegate(this,this.publishSuc);
	var param = {action:"publishHome",catId:catid};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.publishSuc=function(r){
	$("loading").style.visibility = "hidden";
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		alert("Done");
	}
}

NEWSORDER.prototype.deleteNews=function(newsid){
	var fncSuc = XHR.Delegate(this,this.deleteNewsSuc);
	var param = {action:"deleteHome",newsId:newsid,catId:$CURENTCAT};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.deleteNewsSuc=function(r){
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		$TOTALNEWS--;
		listHome($CURENTCAT);
	}
}

NEWSORDER.prototype.updateMode=function(status){
	var fncSuc = XHR.Delegate(this,this.updateModeSuc);
	var param = {action:"updateModeCate",status:status,catId:$CURENTCAT};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.updateModeSuc=function(r){
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		alert("Done");
	}
}

NEWSORDER.prototype.getMode=function(){
	var fncSuc = XHR.Delegate(this,this.getModeSuc);
	var param = {action:"getModeCate",catId:$CURENTCAT};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.getModeSuc=function(r){
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		var cat = xml.getElementsByTagName("cat")[0];
		var mod = cat.getAttribute("mode");
		$N("checkmode")[mod].checked=true;
	}
}

function updateMode(status){
	new NEWSORDER().updateMode(status);
}


NEWSORDER.prototype.updatePosition=function(idList){
	var fncSuc = XHR.Delegate(this,this.updatePositionSuc);
	var param = {action:"updatePosition",idList:idList,catId:$CURENTCAT};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
NEWSORDER.prototype.updatePositionSuc=function(r){
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		listHome($CURENTCAT);
	}else{
		alert("Lỗi! 2 số trùng nhau");
	}
}
NEWSORDER.prototype.listNewsByCat=function(page){
	var fncSuc = XHR.Delegate(this,this.listNewsByCatSuc);
	var param = {action:"listNewsCat",catid:$CATID,page:page,n:10};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.page=page;
}
NEWSORDER.prototype.listNewsByCatSuc=function(r){
	var xml = r.xml;
	var html = "";
	if(getRV(xml)){
		if(this.page==-1){
			var p = xml.getElementsByTagName("page");
			var page = new Page("pagebreak1","pagebreak2",p[0].getAttribute("totalpage"),listNewsCat,1,"pagebreak");
			page.show();
		}
		var html="";
		var il = xml.getElementsByTagName("item");
		for(var i=0; i < il.length; i++){
			if(il[i].getAttribute("homepic")=="null"||il[i].getAttribute("homepic")==""){
				homepic="";
				noImg="<span style='color:red'>&nbsp;(Chưa có ảnh lớn)</span>";
			}else{
				homepic = il[i].getAttribute("homepic");
				noImg="";
			}
			html += "<div class=\'list1\' style=\'cursor:pointer\' id=\"div_"+il[i].getAttribute("newsid")+"\">* " +
					il[i].getAttribute("title")+noImg+
					"<span style=\'font-size:11px;padding-left:10px\'>("+fulldateSQLtoN(il[i].getAttribute("createddate"))+")</span>"+
					"<div style=\'font-size:11px\'><a href=\'"+il[i].getAttribute("link")+"\'>[Xem trang gốc]</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a  target=\'_blank\' href=\'"+makeLinkViewNews(il[i].getAttribute("newsid"),il[i].getAttribute("utitle"))+"\'>[Xem ROLO News]</a></div>"+
					"<div style=\'padding-top:5px\'>" +
					"<span style=\'padding:3px;background-color:#960;color:#ffffff;font-size:11px\' onClick=\'new NEWSORDER().addHome(\""+il[i].getAttribute("newsid")+"\",1)\'>Thêm vào đầu trang</span>" +
					"<span style=\'padding:3px;background-color:#9CF;color:#036;font-size:11px\' onClick=\'new NEWSORDER().addHome(\""+il[i].getAttribute("newsid")+"\",0)\'>Thêm vào cuối trang</span>" +					
					"</div>"+
					"</div>";
		}
		if(html!=""){
			$("listnews2").innerHTML=html;
			try{
				$("div_"+$CUREVENT).style.fontWeight="bold";
			}catch(e){}
		}
		else
			$("listnews2").innerHTML="<div align='center' style='padding:10px'><b>Chưa có dữ liệu!</b></div>";
		}
}

function listNewsCat(page){
	$CATID = $N("catid")[0].value;
	new NEWSORDER().listNewsByCat(page);
}
function showUpload(id,homepic){
	$N("newsId")[0].value = id;
	$N("oldFile")[0].value = homepic
	showDiv('upload_div');
	var pos = findPos($("updateBtn_"+id));
	setPos('upload_div',pos[1],pos[0]);
}
function onUpload(value, msg){
	if(value=="true"){
		listHome($CURENTCAT);
		listNewsCat(-1);
		hideDiv("upload_div");
		document.myform.reset();
	}else{
		alert("Đã có lỗi xảy ra. Vui lòng kiểm tra lại thông tin (ngày tháng, tiêu đề,...)");
	}
	$("loading").style.visibility = "hidden";
}
var $LASTSUB="",$CURENTCAT="";
function clickTabHome(catid){
	var catlist = [0,7];
	for(var i=0; i < catlist.length; i++){
		$("tab_"+catlist[i]).style.backgroundColor="#EBEBEB";
	}
	$HOMECAT = catid;
	$CURENTCAT=$HOMECAT;
	$("tab_"+catid).style.backgroundColor="#CCF";
	if(catid==7){
		$("divmain_7").style.display="block";
	}else{
		$("divmain_7").style.display="none";
		new NEWSORDER().getMode();
		listHome($CURENTCAT);
	}
}
function clickSubTab(catid){
	try{$("subtab_"+$LASTSUB).style.textDecoration="";}
	catch(e){}
	$LASTSUB=catid;
	$CURENTCAT = $LASTSUB;
	$("subtab_"+$LASTSUB).style.textDecoration="underline";
	new NEWSORDER().getMode();
	listHome($CURENTCAT);
	
}
function publishHome(){
	if(confirm("Bạn có chắc muốn Publish?")){
		new NEWSORDER().publish($CURENTCAT);
	}
}
